home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-09-04 | 17.5 KB | 649 lines | [TEXT/MPS ] |
- // UWindoid.cp ------------------------------------------------------------------------
- // Copyright © 1991 by Apple Computer, Inc. All rights reserved.
-
- #pragma once
-
- //-------------------------------------------------------------------------------------
- // INCLUDES
-
- #include "WindoidRez.h" // combined header file for resource IDs
- #include "UWindoid.h" // class definitions
-
-
- //-------------------------------------------------------------------------------------
- // MEMBER FUNCTIONS
-
-
- // TWINDOIDAPPLICATION ----------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TWindoidApplication::IWindoidApplication(OSType itsMainFileType, OSType itsCreator)
- {
- inherited::IApplication(itsMainFileType, itsCreator);
-
- // So my view will be substituted when MacApp® creates the "default view"
- RegisterStdType("TDefaultView", 'dflt');
-
- if (gDeadStripSuppression){ // so the linker doesn't dead strip class info
- macroDontDeadStrip(TDefaultView);
- macroDontDeadStrip(TFloatingMenu);
- macroDontDeadStrip(TLightPalette);
- macroDontDeadStrip(TWindow);
- }
-
- // create the floating palette window
- gFloatMenuWindow = (TWindow *)gViewServer->NewTemplateWindow(kLightPaletteRSRCID, NULL);
- FailNIL(gFloatMenuWindow);
-
- // create the floating tool menu
- gFloatingMenu = new TFloatingMenu;
- gFloatingMenu->IFloatingMenu();
- }
-
-
- #pragma segment AInit
- pascal void TWindoidApplication::DoSetupMenus()
- {
- inherited::DoSetupMenus();
- Enable(cGreen,TRUE);
- Enable(cYellow, TRUE);
- Enable(cRed, TRUE);
- }
-
-
- #pragma segment AOpen
- pascal TDocument* TWindoidApplication::DoMakeDocument(CommandNumber /* itsCommandNumber */,
- TFile* /* itsFile */)
- {
- TWindoidDocument* aWindoidDocument = new TWindoidDocument;
- aWindoidDocument->IDocument();
- return aWindoidDocument;
- }
-
-
- // TWINDOIDDOCUMENT METHODS -----------------------------------------------------------
-
- #pragma segment AInit
- pascal void TWindoidDocument::DoSetupMenus() // setup document related menus
- {
- inherited::DoSetupMenus(); // default menus
- Enable(cNormGreen,TRUE); // normal traffic light menu entries
- Enable(cNormYellow,TRUE);
- Enable(cNormRed,TRUE);
- }
-
-
- #pragma segment ASelCommand
- pascal void TWindoidDocument::DoMenuCommand(CommandNumber aCommandNumber)
- {
- TCommand* cmdToPost = NULL;
-
- switch(aCommandNumber){
- case cNormGreen:
- TGreenCommand *aGreenCommand = new TGreenCommand;
- aGreenCommand->IGreenCommand(this);
- cmdToPost = aGreenCommand;
- break;
- case cNormYellow:
- TYellowCommand *aYellowCommand = new TYellowCommand;
- aYellowCommand->IYellowCommand(this);
- cmdToPost = aYellowCommand;
- break;
- case cNormRed:
- TRedCommand *aRedCommand = new TRedCommand;
- aRedCommand->IRedCommand(this);
- cmdToPost = aRedCommand;
- break;
- default:
- inherited::DoMenuCommand(aCommandNumber);
- }
-
- if(cmdToPost != NULL) this->PostCommand(cmdToPost); // post command
- }
-
-
- #pragma segment AOpen
- pascal void TWindoidDocument::DoMakeViews(Boolean /*forPrinting*/)
- {
- TWindow* aWindow;
- TDefaultView* aView;
- FailInfo fi;
-
- if(fi.Try()){ // start
- aWindow = gViewServer->NewTemplateWindow(kDefaultWindowID, this);
- // create the window
-
- aView = (TDefaultView *)aWindow->FindSubView('DFLT'); // get the only view
- FailNIL(aView); // test
- fView = aView; // store in object
-
- fi.Success(); // OK
- }
- else { // problems
- fi.ReSignal(); // signal about it
- }
- }
-
-
- #pragma segment ADoCommand
- pascal void TWindoidDocument::DoLight(eLights theLight) // call view method
- {
- if(theLight == kGreen)
- fView->DrawGreen();
- else if(theLight == kYellow)
- fView->DrawYellow();
- else if(theLight == kRed)
- fView->DrawRed();
- }
-
-
- // TDEFAULTVIEW METHODS -----------------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TDefaultView::Initialize()
- {
- SetRect(fTrafficRect,kleft, ktop, kright, kbottom); // initialize various fields
- SetRect(fBlackTopRect, kleft, ktop, kright, ktop + 5);
- SetRect(fTrafficLightFrame,kleft+1, ktop+1, kright+1, kbottom+1);
-
- short xvar = (kright -kleft)/5; // define the traffic lights
- short yvar = (kbottom -ktop)/13; // proportional to the view
-
- SetRect(fGreenRect, kleft + xvar, ktop + yvar, // create rects
- kleft + (4 * xvar), ktop + (4 * yvar));
- SetRect(fYellowRect,kleft + xvar, ktop + (5 * yvar),
- kleft + (4 * xvar), ktop + (8 * yvar));
- SetRect(fRedRect, kleft + xvar, ktop + (9 * yvar),
- kleft + (4 * xvar), ktop + (12 * yvar));
-
- this->fTitle = "Windoid example:"; // title
-
- this->fCurrentLight = kGreen; // start with green
-
- inherited::Initialize(); // call the real thing
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::Draw(const VRect&) // draw our Traffic Light here
- {
- if (GetGrafPort());
-
- this->DrawTrafficLight(); // draw the object(s)
- this->DrawLight(); // testing, temp thing
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::DrawTrafficLight() // draw the body
- {
- PenNormal();
- PenSize(8, 8);
- PenPat(qd.black);
- FrameRect(fTrafficRect); // draw a dark gray frame
- FrameRect(fBlackTopRect); // fill top
-
- PenSize(2,2); // frame traffic lights
- FrameRect(fGreenRect);
- FrameRect(fYellowRect);
- FrameRect(fRedRect);
-
- // Set font and size for subsequent display in the window
- TextFont(geneva); // a nice outline font
- TextSize(9);
- MoveTo(20, 20);
- DrawString(fTitle); // draw the title
- PenNormal(); // restore the pen state
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::DrawLight() // select light to draw
- {
- if(this->fCurrentLight == kGreen)
- this->DrawGreen();
- else if (this->fCurrentLight == kYellow)
- this->DrawYellow();
- else if (this->fCurrentLight == kRed)
- this->DrawRed();
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::DrawGreen() // draw green light
- {
- if( this->Focus() ){
- this->fCurrentLight= kGreen;
- PenNormal();
- ForeColor(whiteColor);
- PaintOval(fYellowRect);
- PaintOval(fRedRect);
-
- ForeColor(greenColor);
- PaintOval(fGreenRect);
- PenNormal();
- ForeColor(blackColor);
- }
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::DrawYellow() // draw yellow light
- {
- if( this->Focus() ){
- this->fCurrentLight= kYellow;
- PenNormal();
- ForeColor(whiteColor);
- PaintOval(fGreenRect);
- PaintOval(fRedRect);
-
- ForeColor(yellowColor);
- PaintOval(fYellowRect);
- PenNormal();
- ForeColor(blackColor);
- }
- }
-
-
- #pragma segment Main
- pascal void TDefaultView::DrawRed() // draw red light
- {
- if( this->Focus() ){
- this->fCurrentLight= kRed;
- PenNormal();
- ForeColor(whiteColor);
- PaintOval(fYellowRect);
- PaintOval(fGreenRect);
-
- ForeColor(redColor);
- PaintOval(fRedRect);
- PenNormal();
- ForeColor(blackColor);
- }
- }
-
-
-
-
- // TOOLSELECTCOMMAND METHODS --------------------------------------------------------------
-
- #pragma segment ASelCommand
- pascal void TToolSelectCommand::IToolSelectCommand (TLightPalette* theToolsPalette,
- TLightPalette* theMenuToolsPalette,
- TLightPalette* theFloatingToolsPalette,
- VPoint& theMouse)
- {
- ITearOffMenuViewTracker(cChangeTool, NULL, kCantUndo, kDoesNotCauseChange,
- NULL, theToolsPalette, NULL, theMouse);
-
- this->fCausesChange = FALSE; // don't mark the document as changed yet
- this->fTool = kNoTool; // no tool assigned yet
- this->fExitTracking = FALSE; // no tracking yet
- this->fMenuToolsPalette = theMenuToolsPalette; // keep track of the menu views
- this->fFloatingToolsPalette = theFloatingToolsPalette; // as well as the Floating Window view
- this->fViewConstrain = FALSE; // don't constrain mouse to view limits
- }
-
-
- #pragma segment ADoCommand
- pascal void TToolSelectCommand::DoIt(void)
- {
- // tell the TLightPalette in the menu which tool is selected
- if(fMenuToolsPalette != NULL){
- fMenuToolsPalette->fOldTool = fMenuToolsPalette->fCurrentTool;
- fMenuToolsPalette->fCurrentTool = fTool;
- }
-
- // tell the TLightPalette in the floating window which tool is selected
- if(fFloatingToolsPalette != NULL){
- fFloatingToolsPalette->fOldTool = fFloatingToolsPalette->fCurrentTool;
- fFloatingToolsPalette->SelectNewTool(fTool);
- }
-
- switch(fTool){ // Now post the Menu command which is transformed to a TCommmand inside TDocument
-
- case kGreen:
- gApplication->GetTarget()->HandleMenuCommand(cNormGreen);
- break;
- case kYellow:
- gApplication->GetTarget()->HandleMenuCommand(cNormYellow);
- break;
- case kRed:
- gApplication->GetTarget()->HandleMenuCommand(cNormRed);
- break;
- case kNoTool:
- break;
- default:
- break;
- }
- }
-
-
- #pragma segment ADoCommand
- pascal Boolean TToolSelectCommand::IsDoneTracking(void)
- {
- if(!StillDown() || this->fExitTracking)
- return TRUE;
- else
- return FALSE;
- }
-
-
- #pragma segment ADoCommand
- pascal void TToolSelectCommand::TrackFeedback (TrackPhase aTrackPhase,
- const VPoint& anchorPoint,
- const VPoint& previousPoint,
- const VPoint& nextPoint,
- Boolean mouseDidMove,
- Boolean turnItOn)
- { // give us some feedback, and react to it
- if( (fView != NULL) && (fView->IsShown()) && (mouseDidMove) )
- fView->TrackFeedback(aTrackPhase, anchorPoint,previousPoint, nextPoint,
- mouseDidMove, turnItOn);
-
- if(turnItOn){
- if(fView == (TView *)fMenuToolsPalette) // we have a menu tool view
- fTool = fMenuToolsPalette->fSelectedTool;
- if(fView == (TView *)fFloatingToolsPalette) // we have a floating tool view
- fTool = fFloatingToolsPalette->fSelectedTool;
- }
- }
-
-
- #pragma segment ASelCommand
- pascal TTracker* TToolSelectCommand::TrackMouse(TrackPhase aTrackPhase,
- VPoint& /*anchorPoint */,
- VPoint& /*previousPoint*/,
- VPoint& nextPoint,
- Boolean /*mouseDidMove*/)
- {
- if(gTrackingInMenu){
- if( (fView != NULL) && (fView->IsShown()) )
- this->fExitTracking = !fView->ContainsMouse(nextPoint);
-
- if( (aTrackPhase == trackRelease) && (fExitTracking) )
- return NULL; // no tracker needed
- }
- return this; // default behaviour, return tracker
- }
-
-
- // TGREENCOMMAND METHODS ---------------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TGreenCommand::IGreenCommand(TWindoidDocument* itsDocument)
- {
- fCommandDocument = itsDocument; // keep track of the document
- inherited::ICommand(cNormGreen,itsDocument, FALSE, FALSE, NULL);
- }
-
-
- #pragma segment ADoCommand
- pascal void TGreenCommand::DoIt()
- {
- fCommandDocument->DoLight(kGreen); // send message to document
- inherited::DoIt();
- }
-
-
- // TYELLOWCOMMAND METHODS --------------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TYellowCommand::IYellowCommand(TWindoidDocument* itsDocument)
- {
- fCommandDocument = itsDocument; // keep track of the document
- inherited::ICommand(cNormYellow,itsDocument,FALSE, FALSE, NULL);
- }
-
-
- #pragma segment ADoCommand
- pascal void TYellowCommand::DoIt()
- {
- fCommandDocument->DoLight(kYellow); // send message to document
- inherited::DoIt();
- }
-
-
- // TREDCOMMAND METHODS -----------------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TRedCommand::IRedCommand(TWindoidDocument* itsDocument)
- {
- fCommandDocument = itsDocument; // keep track of the document
- inherited::ICommand(cNormRed,itsDocument,FALSE, FALSE,NULL);
- }
-
-
- #pragma segment ADoCommand
- pascal void TRedCommand::DoIt()
- {
- fCommandDocument->DoLight(kRed); // send message to document
- inherited::DoIt();
- }
-
-
- // TFLOATINGMENU METHODS --------------------------------------------------------------------
-
- pascal void TFloatingMenu::IFloatingMenu()
- {
- TLightPalette* aLightPalette;
-
- ITearOffMenuView(mWindoid, kLightPaletteWidth, kLightPaletteHeight,
- (TWindow *)gFloatMenuWindow);
-
- aLightPalette = new TLightPalette;
- aLightPalette->ILightPalette(NULL, this);
- aLightPalette->fIdentifier = 'MTUL';
- }
-
-
-
- // TLIGHTPALETTE METHODS --------------------------------------------------------------------
-
- #pragma segment AInit
- pascal void TLightPalette::ILightPalette(TDocument* itsDocument, TView* itsSuperView)
- {
- VPoint itsSize;
-
- SetVPt(itsSize, kLightPaletteWidth, kLightPaletteHeight);// inherited
- IView(itsDocument, itsSuperView, gZeroVPt, itsSize, sizeFixed, sizeFixed);
-
- fCurrentTool = kNoTool; // default settings
- fOldTool = kNoTool; // for all the
- fSelectedTool = kNoTool; // tool selection fields
-
- this->BuildChoiceArray(); // build Rects for use in Windoid
- }
-
-
-
- #pragma segment Main
- pascal void TLightPalette::Draw(const VRect& area) // draw palette view contents
- {
- Rect r;
- register long i;
-
- PenNormal();
- PenSize(1,1);
- MoveTo((short)fSize.h - 1, 0); // make a right side drop effect
- Line(0, (short)fSize.v);
-
- for(i = 0; i <= kLightsInPalette; i++){ // frame rects
- r = fChoiceArray[i];
- FrameRect(r);
-
- }
-
- for(i = 0; i <= kLightsInPalette; i++){
- if(i == kGreen){ // first rect == Green
- SetRect(r,5,5,30,30); // define the rect
- ForeColor(greenColor); // specify green
- PaintOval(r); // paint green oval
- ForeColor(blackColor); // set back to black
- }
- else if(i == kYellow){ // second rect == Yellow
- OffsetRect(r,0,33); // offset a little bit
- ForeColor(yellowColor); // specify yellow
- PaintOval(r); // paint yellow oval
- ForeColor(blackColor); // set back to black
- }
- else if(i == kRed){ // third rect = Red
- OffsetRect(r,0,33); // offset a little bit
- ForeColor(redColor); // specify red
- PaintOval(r); // paint red oval
- ForeColor(blackColor); // set back to black
- }
- } // end for loop
-
- PenNormal();
- inherited::Draw(area);
- }
-
-
- #pragma segment AInit
- pascal void TLightPalette::BuildChoiceArray() // build rect array
- {
- short top;
- register long i;
- Rect r;
-
- top = 0;
- for (i =0; i < kLightsInPalette; i++){
- SetRect(r,0, top, kLightPaletteWidth - 1, top + kLightPaletteWidth - 1);
- fChoiceArray[i] = r;
- top = top + kLightPaletteWidth - 1;
- }
- }
-
-
- #pragma segment ARes
- pascal void TLightPalette::Activate(Boolean entering)
- {
- if(!entering) // first time in this view?
- fCurrentTool = kNoTool; // default setting
- inherited::Activate(entering); // do the rest, TView :: Activate..
- }
-
-
- #pragma segment ARes
- pascal void TLightPalette::IRes(TDocument* itsDocument, TView* itsSuperView,
- TStream* itsParams)
- {
- inherited::IRes(itsDocument, itsSuperView, itsParams);
-
- fCurrentTool = kNoTool; // default settings
- fOldTool = kNoTool;
- fSelectedTool = kNoTool;
- fCurrentTool = kNoTool;
-
- this->BuildChoiceArray(); // build Rect array
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::DoMouseCommand(VPoint& theMouse, TToolboxEvent* /*event*/,
- Point /*hysteresis*/)
- {
- TLightPalette* aMenuLightPalette;
- TLightPalette* aFloatingLightPalette;
- TToolSelectCommand* aToolSelectCommand;
-
- aMenuLightPalette = (TLightPalette *)gFloatingMenu->FindSubView('MTUL');
-
- aFloatingLightPalette = NULL;
- Boolean temp = gFloatMenuWindow->IsShown();
-
- if( gFloatMenuWindow != NULL ){ // exists
- aFloatingLightPalette = (TLightPalette *)gFloatMenuWindow->FindSubView('TPLT');
- FailNIL(aFloatingLightPalette);
- }
-
- aToolSelectCommand = new TToolSelectCommand; // create a tool selection command
-
- aToolSelectCommand->IToolSelectCommand(this, aMenuLightPalette,
- aFloatingLightPalette, theMouse);
-
- PostCommand(aToolSelectCommand); // post the tool select command
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::DoHighlightSelection(HLState /*fromHL*/, HLState toHL)
- {
- if( (fCurrentTool != kNoTool) && (fCurrentTool < kLightsInPalette) ){ // valid selection
- if(toHL == hlOn) // turn on selected tool
- this->Toggle();
-
- if(toHL == hlOff) // turn off selected tool
- this->Toggle(); // turn it off
- }
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::Toggle(void) // toggle between tool rects
- {
- Rect r;
-
- r = fChoiceArray[fCurrentTool]; // get the tool
- UseSelectionColor(); // hilite with the selected color
- InvertRect(r); // invert
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::FrameTool(Rect& r) // frame selected tool rect
- {
- PenNormal();
- PenMode(patXor);
- PenSize(2,2);
- FrameRect(r);
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::SelectNewTool(eLights whichTool)
- {
- Boolean foo =this->IsShown();
-
- if( this->IsShown() ){ // check if valid floating window
- if( this->Focus() ){
- this->DoHighlightSelection(hlOn,hlOff); // turn current selection off
- fCurrentTool = whichTool; // define new current tool
- this->DoHighlightSelection(hlOff,hlOn); // turn new selection on
- }
- }
- }
-
-
- #pragma segment ASelCommand
- pascal void TLightPalette::TrackFeedback( TrackPhase /*aTrackPhase*/,
- const VPoint& /*anchorPoint*/,
- const VPoint& /*previousPoint*/,
- const VPoint& nextPoint,
- Boolean mouseDidMove,
- Boolean turnItOn)
- {
- Point aPnt;
- Rect r;
- register long i;
-
- if(mouseDidMove){
- if( this->Focus() ){
- aPnt = ViewToQDPt(nextPoint);
- for(i = 0; i < kLightsInPalette; i++){ // iterate
- r = fChoiceArray[i];
- if( PtInRect(aPnt,r) ){
- this->FrameTool(r); // frame selected tool
- if(turnItOn)
- fSelectedTool = (eLights)i; // the tool selected
- else
- fSelectedTool =(eLights)kNoTool;// no tool selected
- }
- }
- }
- }
- }
-
-
-